package leetcode.code2531;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {
	
	public boolean isItPossible(String word1, String word2) {
		int[] c1 = counter(word1);
		int[] c2 = counter(word2);
		int len1 = cnt(c1);
		int len2 = cnt(c2);
		for (int i = 0; i < c1.length; i++) {
			if (c1[i] == 0) {
				continue;
			}
			for (int j = 0; j < c2.length; j++) {
				if (c2[j] == 0) {
					continue;
				}
				if (i == j && len1 == len2)
					return true;
				if (i != j) {
					int cur1 = len1;
					cur1 += word1.indexOf((char) (j + 'a'), 0) < 0 ? 1 : 0;
					cur1 -= c1[i] == 1 ? 1 : 0;
					int cur2 = len2;
					cur2 += word2.indexOf((char) (i + 'a'), 0) < 0 ? 1 : 0;
					cur2 -= c2[j] == 1 ? 1 : 0;
					if (cur1 == cur2)
						return true;
				}
			}
		}
		return false;
	}

	private int cnt(int[] c1) {
		int ans = 0;
		for (int n : c1) {
			ans += n == 0 ? 0 : 1;
		}
		return ans;
	}

	private int[] counter(String word1) {
		int[] ans = new int[26];
		for (char c : word1.toCharArray()) {
			ans[c - 'a']++;
		}
		return ans;
	}
	

	@Override
	public void debug4() {
		H.compare(false, this.isItPossible("ac", "b"));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}

